﻿namespace Resonator.Domain.Models
{
	using System;
	using System.Diagnostics;
	using System.Text;
	using Castle.ActiveRecord;
	using NHibernate.Search.Attributes;
	using Field=NHibernate.Search.Attributes.FieldAttribute;

	[Serializable]
	[ActiveRecord]
	[Indexed]
	[DebuggerDisplay("Phrase={Phrase} LanguageName={LanguageName}")]
	public class Translation : LocalizableModel
	{
		private Resource _resource;
		private string _phrase = string.Empty;

		[BelongsTo]
		[ContainedIn]
		public virtual Resource Resource
		{
			get { return _resource; }
			set { _resource = value; }
		}

		[Property(NotNull = true)]
		[Field(Index.Tokenized, Store = Store.No)]
		public virtual string Phrase
		{
			get { return _phrase; }
			set { _phrase = value ?? string.Empty; }
		}

		protected override void GetFullText(StringBuilder builder)
		{
			builder.Append(Phrase);
			builder.Append(' ');

			base.GetFullText( builder );
		}

		public static Translation Copy(Translation copyFrom)
		{
			Translation translation = new Translation();
			translation.Culture = copyFrom.Culture;
			translation.Phrase = copyFrom.Phrase;
			return translation;
		}

		public static Translation FromResource(Resource resource)
		{
			Translation translation = new Translation();
			translation.Culture = resource.Culture;
			translation.Phrase = resource.Phrase;
			return translation;
		}
	}
}